package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.dao.BookDao;
import com.itheima.domain.Book;
import com.itheima.service.IBookService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * className: BookServiceImpl
 * description:
 * date: 2023/2/22 0022-PM 11:15:35
 * <p>
 * project: springboot
 * package: com.itheima.service.impl
 * email: 1085844536@qq.com
 * version:
 *
 * @author WangGuojian
 */
@Service
public class BookServiceImpl extends ServiceImpl<BookDao, Book> implements IBookService {
    @Autowired
    private BookDao bookDao;

    /**
     * 保存
     *
     * @param book {@link Book}
     * @return {@link Boolean}
     */
    @Override
    public boolean saveBook(Book book) {
        return bookDao.insert(book) > 0;
    }

    /**
     * 修改
     *
     * @param book {@link Book}
     * @return {@link Boolean}
     */
    @Override
    public boolean modify(Book book) {
        return bookDao.updateById(book) > 0;
    }

    /**
     * 删除
     *
     * @param id {@link Integer}
     * @return {@link Boolean}
     */
    @Override
    public boolean delete(Integer id) {
        return bookDao.deleteById(id) > 0;
    }

    /**
     * 分页查询
     *
     * @param currentPage {@link Integer}
     * @param pageSize    {@link  Integer}
     * @return {@link IPage <Book>}
     */
    @Override
    public IPage<Book> getPage(int currentPage, int pageSize) {
        IPage<Book> page = new Page<>();
        bookDao.selectPage(page, null);
        return page;
    }

    /**
     * 分页查询
     *
     * @param currentPage {@link Integer}
     * @param pageSize    {@link  Integer}
     * @param book        {@link Book}
     * @return {@link IPage<Book>}
     */
    @Override
    public IPage<Book> getPage(int currentPage, int pageSize, Book book) {
        LambdaQueryWrapper<Book> lqw = new LambdaQueryWrapper<>();
        lqw.like(Strings.isNotEmpty(book.getType()), Book::getType, book.getType());
        lqw.like(Strings.isNotEmpty(book.getName()), Book::getName, book.getName());
        lqw.like(Strings.isNotEmpty(book.getDescription()), Book::getDescription, book.getDescription());
        IPage<Book> page = new Page<>();
        bookDao.selectPage(page, lqw);
        return page;
    }
}
